package com.zyc.app.woa.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.zyc.app.woa.R;
import com.zyc.app.woa.WxConfig;
import com.zyc.app.woa.db.DBManager;
import com.zyc.app.woa.utils.AsyncTaskExecutor;
import com.zyc.app.woa.utils.Caches;
import com.zyc.app.woa.utils.Caches.CacheKey;
import com.zyc.app.woa.utils.CommonUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by zyc on 12/27/14.
 */
public class SettingsActivity extends BaseActivity {
    private TextView mTotalArticles;
    private TextView mCleanAfterReading;
    private TextView mCacheInfo;
    //    private TextView mClearCache;
    private boolean mClear = true;
    public static final int MSG_GET_ARTICLES_NUM = 100;
    public static final int MSG_GET_CACHED_SIZE = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_layout);
        mTotalArticles = (TextView) findViewById(R.id.total_articles);
        mCleanAfterReading = (TextView) findViewById(R.id.clear_after_reading);
        mCacheInfo = (TextView) findViewById(R.id.cache_info);
//        mClearCache = (TextView) findViewById(R.id.clear_caches);

        mClear = Caches.getBoolean(CacheKey.CLEAR_AFTER_READ, true);
        updateCheckStatus(mClear);
        mCleanAfterReading.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mClear = !mClear;
                Caches.put(CacheKey.CLEAR_AFTER_READ, mClear);
                updateCheckStatus(mClear);
            }
        });

        mTotalArticles.setText(getString(R.string.total_articles, 0));
        AsyncTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                mHandler.sendMessage(mHandler.obtainMessage(MSG_GET_ARTICLES_NUM, DBManager.getTotalArticleCount()));
            }
        });

        mCacheInfo.setText(getString(R.string.caches_info, 0));
        AsyncTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final DecimalFormat decimalFormat = new DecimalFormat(".0");
                String cacheSize = decimalFormat.format((1.0d * FileUtils.sizeOfDirectory(new File(WxConfig.CACHE_ROOT_PATH)) / WxConfig.MB_SIZE));
                String freeSize = decimalFormat.format(1.0d * CommonUtils.getSdCardFreeSpace() / WxConfig.MB_SIZE);
                mHandler.sendMessage(mHandler.obtainMessage(MSG_GET_CACHED_SIZE, cacheSize + " / " + freeSize));
            }
        });

    }

    private void updateCheckStatus(boolean status) {
        int drawableRes = status ? R.drawable.ic_check : R.drawable.ic_close;
        mCleanAfterReading.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_GET_ARTICLES_NUM:
                    mTotalArticles.setText(getString(R.string.total_articles, msg.obj));
                    break;
                case MSG_GET_CACHED_SIZE:
                    mCacheInfo.setText(getString(R.string.caches_info, msg.obj));
                    break;
                default:
                    break;
            }
        }
    };
}
